package com.hibase.core.interfacetask.controller;


import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.interfacetask.entity.MdsInterfaceTask;
import com.hibase.core.interfacetask.entity.vo.*;
import com.hibase.core.interfacetask.service.IMdsInterfaceTaskService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.common.exception.HibaseAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hufeng
 * @since 2019-04-29
 */
@RestController
@RequestMapping("/mds-interface-task")
@Api(tags = "接口管理")
public class MdsInterfaceTaskController {

    @Autowired
    private IMdsInterfaceTaskService mdsInterfaceTaskService;

    @ApiOperation(value = "查询接口任务表列表", httpMethod = "POST" , response = MdsInterfaceTaskListVO.class)
    @HibasePostRequestMapping(value = "selectMdsInterfaceTaskList")
    public ResponseModel<MdsInterfaceTaskListVO> selectMdsInterfaceTaskList(@RequestBody MdsInterfaceTaskSearchVO searchVO) {

        PageInfo result = mdsInterfaceTaskService.selectMdsInterfaceTaskList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), MdsInterfaceTaskListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增接口任务表记录", httpMethod = "POST",notes = "返回提示修改成功或失败")
    @HibasePostRequestMapping(value = "addMdsInterfaceTask")
    public ResponseModel<String> addMdsInterfaceTask(@RequestBody MdsInterfaceTaskInsertVO insertVO) {

        MdsInterfaceTask entity = EntityUtil.transform(insertVO, MdsInterfaceTask.class);

        mdsInterfaceTaskService.saveMdsInterfaceTask(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改接口任务表记录", httpMethod = "POST",notes = "返回提示修改成功或失败")
    @HibasePostRequestMapping(value = "updateMdsInterfaceTask")
    public ResponseModel<String> updateMdsInterfaceTask(@RequestBody MdsInterfaceTaskUpdateVO updateVO) {

        MdsInterfaceTask entity = EntityUtil.transform(updateVO, MdsInterfaceTask.class);

        mdsInterfaceTaskService.saveMdsInterfaceTask(entity);

        return ResponseModel.successMsg(entity.getId());
    }




    @ApiOperation(value = "查询接口任务表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewMdsInterfaceTaskDetails")
    public ResponseModel<MdsInterfaceTaskViewVO> viewMdsInterfaceTaskDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(mdsInterfaceTaskService.viewMdsInterfaceTask(operationidEntity.getId()), MdsInterfaceTaskViewVO.class));
    }

    @ApiOperation(value = "立刻执行", httpMethod = "POST")
    @HibasePostRequestMapping(value = "executeTask")
    public ResponseModel<String> executeTask(@RequestBody OperationIdsEntity operationIdsEntity) {
        HibaseAssert.notNull(operationIdsEntity,"传参为空");
        mdsInterfaceTaskService.executeTask(operationIdsEntity.getIds());
        return ResponseModel.successMsg("执行成功");
    }

    @ApiOperation(value = "异步调用接口", httpMethod = "POST")
    @HibasePostRequestMapping(value = "asyncCall")
    public ResponseModel<String>  asyncCall(@RequestBody MdsInterfaceTaskThirdCallVO mdsInterfaceTaskThirdCallVO) {
        HibaseAssert.notNull(mdsInterfaceTaskThirdCallVO,"传参为空");
        mdsInterfaceTaskService.asyncCall(mdsInterfaceTaskThirdCallVO,null);
        return ResponseModel.successMsg("调用成功");
    }

    @ApiOperation(value = "同步调用接口", httpMethod = "POST")
    @HibasePostRequestMapping(value = "syncCall")
    public ResponseModel<String> syncCall(@RequestBody MdsInterfaceTaskThirdCallVO mdsInterfaceTaskThirdCallVO) {

        return ResponseModel.successMsg(mdsInterfaceTaskService.syncCall(mdsInterfaceTaskThirdCallVO));
    }


    @ApiOperation(value = "忽略失败的接口任务",httpMethod = "POST")
    @HibasePostRequestMapping(value = "ignore")
    public ResponseModel<String> ignore(@RequestBody OperationIdsEntity idsEntity){

        mdsInterfaceTaskService.ignore(idsEntity.getIds(),idsEntity.getRemarks());
        return ResponseModel.successMsg();
    }

}
